package com.wy.springbootblog.controller;

import com.wy.springbootblog.pojo.Comment;
import com.wy.springbootblog.pojo.Result;
import com.wy.springbootblog.pojo.SearchBean;
import com.wy.springbootblog.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    // 新增评论
    @PostMapping("/add")
    public Result<Void> add(@RequestBody @Validated Comment comment) {
        return commentService.saveComment(comment);
    }

    // 删除评论
    @DeleteMapping("/delete/{id}")
    public Result<Void> delete(@PathVariable @NotNull(message = "评论id不能为空") Integer id) {
        return commentService.deleteComment(id);
    }

    // 分页查询评论列表
    @PostMapping("/list")
    public Result list(@RequestBody @Validated SearchBean<Comment> searchBean) {
        return commentService.commentList(searchBean);
    }

    // 修改评论
    @PostMapping("/update")
    public Result<Void> update(@RequestBody @Validated Comment comment) {
        return commentService.updateComment(comment);
    }
}
